package com.common.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;

import com.base.common.mvc.BaseInterceptor;
import com.base.core.mvc.core.LoginManager;
import com.base.service.base.logger.entity.LoginLogDO;
import com.base.service.base.logger.service.LoginLogService;
import com.base.service.system.dto.JwtUserDTO;
import com.base.service.system.entity.UserDO;
import com.base.service.system.service.UserService;
import com.base.service.system.service.impl.UserServiceImpl;
import com.gitee.magic.framework.base.context.Http;

/**
 * 拦截器
 * @author start
 *
 */
public class AuthorityInterceptor extends BaseInterceptor {
	
	@Autowired
	private LoginManager loginManager;

	@Autowired
	private UserService userService;
	@Autowired
	private LoginLogService loginLogService;
	
	public LoginLogDO getLoginLog(Http http,String userType) {
		JwtUserDTO jwtU=loginManager.analysisJwt(JwtUserDTO.class,false);
		http.setAccessId(jwtU.getAccessId());
		http.requestCheck(false);
		http.putCache(jwtU);
		return loginLogService.authority(jwtU.getAccessId(),userType);
	}
	
	@Override
	public void authenticationCheck(Http http, HandlerMethod handlerMethod) {
		LoginLogDO log = getLoginLog(http,UserServiceImpl.USER);
		
		UserDO user = userService.load(log.getUserId());
		userService.checkUserNotDisable(user);
		// AuthenticationCheck check = handlerMethod.getMethodAnnotation(AuthenticationCheck.class);
		// if(check.value()) {
		// }
		http.putCache(log);
		http.putCache(user);
	}
	
}
